#include <QtGui>
#include <QtXml>
#include "attribute.h"
#include "attributesmodel.h"
int Attribute::num=1;

Attribute::Attribute(const QString &text, QObject *parent,QString name, QString type, int nom)
    : QObject(parent)
{
 //   setAttribute(Qt::WA_DeleteOnClose);
    //QMessageBox::warning(0,"debug",tr("%1").arg(nom));
    // номер домена
    
	if(nom==0)
   	     _nom = Attribute::num++;
	else _nom = nom;
	_alias = text;
	_name  = name;
	_type  = type;
	_length = 0;
	_idconf = 0;
	_opened = 0;	

	_used  = false;
	_plus  = false;
	_selected = false;
	//dataOut();	    
}
//
QPixmap Attribute::dataOut()
{
    QString text = _alias;//+(_plus?"...":"");
    QFont font(qApp->font());
	//QFont font(QFont("Times", 10));
    QFontMetrics metric(font);
	if(_plus) font.setBold(true);
    QSize size = metric.size(Qt::TextSingleLine, text)+QSize(30+20,0); // 30 - ширина для показа номера
	
    QImage image(size.width() + 12, size.height() + 2,
                 QImage::Format_ARGB32_Premultiplied);
    image.fill(qRgba(0, 0, 0, 0));


    font.setStyleStrategy(QFont::ForceOutline);

    QPainter painter;
    painter.begin(&image);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(Qt::white);
    if(!_used) painter.setPen(Qt::blue);
   	else painter.setPen(Qt::green);
	if(_selected) painter.setBrush(Qt::white);
	painter.setFont(font);
    //painter.drawRoundRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1),25, 25);
	//painter.drawRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1));
    //painter.drawLine(QPoint(30,0),QPoint(30,image.height()));
    //painter.drawLine(QPoint(image.width()-20,0),QPoint(image.width()-20,image.height()));
	//painter.setPen(Qt::black);
	
    
	
    //painter.setBrush(Qt::black);
    //painter.drawText(QRect(QPoint(0, 0), QPoint(30,image.height())), Qt::AlignCenter, QString("%1").arg(_nom));
	int h = 1;
    //painter.drawText(QRect(QPoint(15, h), size-QSize(30+20,0)), Qt::AlignCenter, text);
	//painter.drawText(QRect(QPoint(10, h), size-QSize(30+20,0)), Qt::AlignCenter, text);
	painter.drawText(QRect(QPoint(10, h), size), Qt::AlignLeft, text);
    //painter.drawText(QRect(QPoint(size.width()-8, h), QSize(20,size.height())),Qt::AlignCenter,_plus?"+":"-");
	//painter.drawText(QRect(QPoint(10, h), QSize(20,size.height())),Qt::AlignCenter,_plus?"+":"-");
    //painter.drawText(QRect(QPoint(15, h), size), Qt::AlignCenter, text+(_plus?"...":""));
    

    painter.end();

	return QPixmap::fromImage(image);
}
//
//
//
void Attribute::xml(QDomNode &node)
{
	QDomDocument doc;
	QDomNode tmpnode;
	QDomElement elem,xelem;
	xelem = doc.createElement("attribute");
// имя
	xelem.setAttribute("name",_name);
	//node.appendChild(elem);
// псевдоним
	tmpnode = doc.createTextNode(_alias);
	elem = doc.createElement("id");
	elem.appendChild(tmpnode);
	xelem.appendChild(elem);
// тип
	tmpnode = doc.createTextNode(_type);
	elem = doc.createElement("type");
	elem.appendChild(tmpnode);
	xelem.appendChild(elem);
// номер
	tmpnode = doc.createTextNode(QString("%1").arg(_nom));
	elem = doc.createElement("order");
	elem.appendChild(tmpnode);
	xelem.appendChild(elem);	
// длина
	tmpnode = doc.createTextNode(QString("%1").arg(length()));
	elem = doc.createElement("length");
	elem.appendChild(tmpnode);
	xelem.appendChild(elem);	
	node.appendChild(xelem);
// режим присвоения идентификационных номеров
	tmpnode = doc.createTextNode(QString("%1").arg(idConf()));
	elem = doc.createElement("idconf");
	elem.appendChild(tmpnode);
	xelem.appendChild(elem);	
	node.appendChild(xelem);
// список значений атрибутов
	tmpnode = doc.createTextNode(QString("%1").arg(opened()));
	elem = doc.createElement("opened");
	elem.appendChild(tmpnode);
	xelem.appendChild(elem);	
	node.appendChild(xelem);
}
//
void Attribute::slotEdit()
{

}
//
//
//
void Attribute::mouseDoubleClickEvent(QMouseEvent * /*event*/)
{
// по двойному щелчку - редактирование
	slotEdit();
}
//
int Attribute::order()
{
	return _nom;
}
//
void Attribute::setPlus(bool plus)
{
	if(_plus != plus){
		_plus = plus;
		//dataOut();
	}
}
//
void Attribute::setUsed(bool used)
{ 
	if(_used != used){
		_used = used;
		//dataOut();	
	}
}
void Attribute::copyLabel(Attribute *label)
{
   	setPlus(label->plus());
	setUsed(label->used());
	setLength(label->length());
	setIdConf(label->idConf());
	setOpened(label->opened());
		
}
//
void Attribute::setOrder(int order)
{
	int oldnom = _nom;
    _nom=order;	
	emit attrChanged(oldnom,this);  
}
//
QDomElement Attribute::serverFormat()
{
	QDomDocument doc;
	QDomElement elem,type,open,form,alias;
	elem = doc.createElement("attribute");
	elem.setAttribute("name",_name);
	elem.setAttribute("alias",_alias);
	elem.setAttribute("type",_type);	
	elem.setAttribute("open",QString("%1").arg(_opened));
	elem.setAttribute("form",QString("%1").arg(_idconf));
	return elem;	
}

void Attribute::setAlias(const QString &val)
{
	_alias = val;
	//dataOut();
}

void Attribute::setName(const QString &val)
{
	_name = val;
	//dataOut();
}

void Attribute::setType(const QString &val)
{
	_type = val;
	dataOut();
}

void Attribute::setSelected(bool s)
{
	_selected = s;
	//dataOut();
}

QFont Attribute::font()
{
	QFont font(qApp->font());	
	font.setStyleStrategy(QFont::ForceOutline);
	if(_plus) font.setBold(true);
	return font;
}

QColor Attribute::color()
{
	QColor color;
    if(!_used) color = Qt::blue;
   	else color = Qt::green;
	return color;
}
